#!/bin/bash  
  
# 检查是否提供了足够的参数  
if [ "$#" -ne 2 ]; then  
    echo "Usage: $0 <m3u8url> <filename>"  
    exit 1  
fi  
  
# 获取参数  
M3U8_URL=$1  
FILENAME=$2

# python3 m3u.py  
  
# 调用Python脚本
#command=$(python3 m3u.py "$M3U8_URL" "$FILENAME")

python3 m3u.py "$M3U8_URL" "$FILENAME"

# 检查上一个命令的退出状态  
if [ $? -eq 0 ]; then  
    echo "命令成功执行。"  

    'ret.data'

    if [ ! -f "./m3u8dlDownloads/$FILENAME/ret.data" ]; then  
        echo "文件 $filename 不存在."  
        exit 1  
    else
        echo "ret.data存在，开始转码"
        ffmpeg -allowed_extensions ALL -protocol_whitelist 'file,http,crypto,tcp' -i "./m3u8dlDownloads/$FILENAME/index.m3u8" -c copy "./m3u8dlDownloads/$FILENAME.mp4"
    fi 

else  
    echo "命令执行失败，退出码：$?"  
fi

# echo "$command"
# if [ "$command" -eq 1 ]; then 
#     ffmpeg -allowed_extensions ALL -protocol_whitelist 'file,http,crypto,tcp' -i "./m3u8dlDownloads/$FILENAME/index.m3u8" -c copy "./m3u8dlDownloads/$FILENAME.mp4"
# else
#     echo "下载失败"
# fi

echo "ended "


# 测试url
# http://ffzy3.tv/index.php/vod/search.html?wd=边水往事
# https://vip.ffzy-video.com/20240823/1106_c9f95a0a/index.m3u8
